
package mduel;

//import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.util.HashMap;


public class Player extends MobileSprite{

int P_ID = 0, state_frame = 0, frame_time = 0;
Pstate current_state = Pstate.ST_STANDING;

int /*init_x = 0,*/ init_y = 0;
boolean facing_right = true, is_rolling = false;
Player next = null;
HashMap sprites;
SpriteBank images;
BufferedImage pPic;
PKeyInfo pki;

//	============================Constructors
public Player(int leftKey, int rightKey, int upKey, int downKey, int actionKey, Player prevP, SpriteBank bank)
    {
	super(true, 0,0, 40, 80);
	pki = new PKeyInfo(leftKey,rightKey,upKey,downKey,actionKey);

    if (prevP == null)
        {
        P_ID = 1;
        }
    else
        {
        prevP.next = this;
        P_ID = prevP.P_ID + 1;
        }
    images = bank;
    pPic = images.retrieveImage("stand");
    }
public Player(int leftKey, int rightKey, int upKey, int downKey, int actionKey, SpriteBank bank)
    {
    this(leftKey, rightKey, upKey, downKey, actionKey, null, bank);
    }
public Player(SpriteBank bank)
    {
    this(KeyEvent.VK_LEFT, KeyEvent.VK_RIGHT, KeyEvent.VK_UP, KeyEvent.VK_DOWN, KeyEvent.VK_CONTROL, bank);
    }
public Player(int start_x, int start_y, SpriteBank bank)
    {
    this(KeyEvent.VK_LEFT, KeyEvent.VK_RIGHT, KeyEvent.VK_UP, KeyEvent.VK_DOWN, KeyEvent.VK_CONTROL, bank);
    super.Xpos = start_x;
    super.Ypos = start_y;
    init_y = start_y;
    }

//============================key setting changers

public PKeyInfo getPKI()
    {
    return pki;
    }

public void setAllPKstate(KeyEvent ke, boolean state)
	{if(!pki.setKeyStates(ke, state))
		next.setAllPKstate(ke, state);}

//============================new player
public Player addPlayer(int leftKey, int rightKey, int upKey, int downKey, int actionKey)
    {
    if (next == null)
        {
        next = new Player(leftKey, rightKey, upKey, downKey, actionKey, this, images);
        }
    else
        {
        next.addPlayer(leftKey, rightKey, upKey, downKey, actionKey);
        }
    return this;
    }


public void updateSelf()
    {
    //remember to switch +/- with y values
    if(super.Ypos < init_y)
        {
        super.Yvel++;
        }
    if((super.Ypos > init_y) || ((super.Ypos+super.Yvel) > init_y))
        {
        super.Ypos = init_y;
        super.Yvel = 0;
        }
    if(pki.isU_pressed() && (super.Ypos == init_y))
        {
    	super.Yvel = -8;
        pPic = getDirImage("jump");
        }
    super.Ypos = super.Ypos + super.Yvel;
    
    if(pki.isR_pressed() ^ pki.isL_pressed())
        {
        if(super.Ypos == init_y)
            {
            frame_time++;
            if(frame_time == 3)
                {
                frame_time = 0;
                state_frame ++;
                }
            
            if(pki.isR_pressed())
                {
                super.Xvel = 2;
                facing_right = true;
                }   
            if(pki.isL_pressed())
                {
            	super.Xvel = -2;
                facing_right = false;
                }
            
            if(pki.isD_pressed())
                {
                if(!is_rolling)
                    {
                    state_frame = 0;
                    is_rolling = true;
                    }
                pPic = rollImage();
                }
            else
                {
                pPic = runImg();
                }
            }
        }
    else
        {
        if(!pki.isD_pressed() && (super.Ypos >= init_y))
            {
        	super.Xvel = 0;
            pPic = getDirImage("stand");
            }
        if(!(pki.isR_pressed() ^ pki.isL_pressed()) && pki.isD_pressed() && (super.Ypos >= init_y))
            {
            super.Xvel = 0;
            pPic = getDirImage("roll1");
            }
        }
    
    super.Xpos = super.Xpos + super.Xvel;


    if (next != null)
        {
        next.updateSelf();
        }
    }
public void drawSelf(Graphics g, ImageObserver IO)
    {
    //g.setColor(Color.yellow);
    //g.fillRect(super.Xpos, super.Ypos, 60, 80);
	//Graphics2D g2 = (Graphics2D) g;
	//(34 x 46)
	//run2 = frame 3 & run3 = frame 2
    /*int frames = 3;
	if(current_state == ST_MOVING)
    	{
        if(facing_right)
        {
    		if(((state_frame%frames)== 1))
    		{
    			g.drawImage(images.loadImage("Rrun1.gif"), super.Xpos, super.Ypos, IO);
    		}
    		else if((state_frame%frames)== 2)
    		{
    			g.drawImage(images.loadImage("Rrun3.gif"), super.Xpos, super.Ypos, IO);
    		}
    		else if((state_frame%frames)== 0)
    		{
    			g.drawImage(images.loadImage("Rrun2.gif"), super.Xpos, super.Ypos, IO);
    		}
        }
        if(!facing_right)
        {
            if(((state_frame%frames)== 1))
            {
                g.drawImage(images.loadImage("Lrun1.gif"), super.Xpos, super.Ypos, IO);
            }
            else if((state_frame%frames)== 2)
            {
                g.drawImage(images.loadImage("Lrun3.gif"), super.Xpos, super.Ypos, IO);
            }
            else if((state_frame%frames)== 0)
            {
                g.drawImage(images.loadImage("Lrun2.gif"), super.Xpos, super.Ypos, IO);
            }
            
        }*/
	//}
    /*else if(current_state == ST_JUMPING)
        {
        if(facing_right)
            {
            g.drawImage(images.loadImage("Rjump.gif"), super.Xpos, super.Ypos, IO);
            }
        if(!facing_right)
            {
            g.drawImage(images.loadImage("Ljump.gif"), super.Xpos, super.Ypos, IO);
            }
        }
	else
        {
        if(facing_right)
            {
            g.drawImage(images.loadImage("Rstand.gif"), super.Xpos, super.Ypos, IO);
            }
        else
            {
            g.drawImage(images.loadImage("Lstand.gif"), super.Xpos, super.Ypos, IO);
            }
        }
    if (next != null)
        {
        next.drawSelf(g, IO);
        }*/
    g.drawImage(pPic, (int)super.Xpos, (int)super.Ypos, IO);
    }
private BufferedImage runImg()
    {
    int frames = 4;
    if(((state_frame%frames)== 0))
            {
            return getDirImage("run4");
            }
        else if((state_frame%frames)== 1)
            {
            return getDirImage("run3");
            }
        else if((state_frame%frames)== 2)
            {
            return getDirImage("run2");
            }
        else
            {
            return getDirImage("run1");
            }
    }
private BufferedImage rollImage()
    {
    int frames = 4;
    if(((state_frame%frames)== 0))
        {
            return getDirImage("roll1");
        }
        else if((state_frame%frames)== 1)
        {
            return getDirImage("roll2");
        }
        else if((state_frame%frames)== 2)
            {
                return getDirImage("roll3");
            }
        else
        {
            is_rolling = false;
            return getDirImage("roll4");
        }
    }
private BufferedImage getDirImage(String img)
    {
    if(facing_right)
        {
        return images.retrieveImage(img);
        }
    else
        {
        return flip(images.retrieveImage(img));
        }
    }
public static BufferedImage flip(BufferedImage src)
    {
    AffineTransform at = AffineTransform.getScaleInstance(-1, 1);
    at.translate(-src.getWidth(null), 0);
    return  new AffineTransformOp(at, AffineTransformOp.TYPE_NEAREST_NEIGHBOR).filter(src, null);
    }
/*public void move()
    {

    if (r_pressed)
        super.Xpos += 2;

    if(l_pressed)
        super.Xpos -= 2;
    }*/

/*public void jump(double height)
    {
    if (state_frame <= 0)
        {
        init_x = super.Xpos;
        init_y = super.Ypos;
        if(r_pressed)
            {
            dir = 1;
            }
        if(l_pressed)
            {
            dir = -1;
            }
        if(!l_pressed && !r_pressed)
            {
            dir = 0;
            }
        }
    if (state_frame <= 8) 
        {
        this.super.Xpos = (int) (state_frame * 2 * dir) + init_x;
        this.super.Ypos = (int) (height * (Math.pow((state_frame * 2 - 8), 2) / 2 - 32)) + init_y;
        }
    else
        {
        current_state = ST_NOTHING;
        state_frame = -1;
        }
    state_frame++;

    }*/
}
